package com.example.blog.controller;

import com.example.blog.model.Article;
import com.example.blog.model.UserInfo;
import com.example.blog.service.ArticleService;
import com.example.blog.util.ConstVariable;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: chengbaiyi
 * @create: 2022-08-25 20:00
 */
@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 所有文章
     * @param cp 当前页码
     * @param ps 每页显示的文章数量
     * @return 文章列表
     */
    @RequestMapping("/artlist")
    public List<Article> articleList(Integer cp, Integer ps) {
        List<Article> data = new ArrayList<>();
        if (cp > 0 && ps > 0) {
            data = articleService.articleList(ps, (cp - 1) * ps);
            for (Article article : data) {
                // 简介
                String context = article.getContext();
                int subLen = Math.min(20, article.getContext().length());
                article.setDesc(context.substring(0, subLen).replace("#", " "));
            }
        }
        return data;
    }


    /**
     * 我的博客列表
     * @param request 请求
     * @return 我的博客列表
     */
    @RequestMapping("/myartlist")
    public List<Article> myArticleList(HttpServletRequest request) {
        List<Article> data = new ArrayList<>();
        // 得到当前登录用户
        UserInfo userInfo = (UserInfo) request.getSession(false).getAttribute(ConstVariable.USER_SESSION_KEY);
        if (userInfo != null) {
            // 根据uid查询当亲登录用户的所有文章
            data = articleService.myArticleList(userInfo.getUid());
            for (Article article : data) {
                // 简介
                String context = article.getContext();
                int subLen = Math.min(20, article.getContext().length());
                article.setDesc(context.substring(0, subLen).replace("#", " "));
            }
        }
        return data;
    }

    /**
     * 添加文章
     * @param request 请求
     * @param title 标题
     * @param context 简介
     * @return 是否添加成功
     */
    @RequestMapping("/addart")
    public int addArticle(HttpServletRequest request, String title, String context) {
        int data = 0;
        // 得到当前登录用户
        UserInfo userInfo = (UserInfo) request.getSession(false).getAttribute(ConstVariable.USER_SESSION_KEY);
        if (userInfo != null) {
            data = articleService.addArticle(title, context, userInfo.getUid());
        }
        return data;
    }

    /**
     * 阅读量+1
     * @param aid aid
     * @return 受影响的行数
     */
    @RequestMapping("/addreadcount")
    public int addReadCount(Integer aid) {
        int data = 0;
        if (aid > 0) {
            data = articleService.addReadCount(aid);
        }
        return data;
    }

    /**
     * 文章详情
     * @param aid aid
     * @return 文章详情
     */
    @RequestMapping("/artdetail")
    public Article artDetail(Integer aid) {
        Article data = new Article();
        if (aid > 0) {
            data = articleService.artDetail(aid);
        }
        return data;
    }

    /**
     * 删除文章
     * @param aid aid
     * @param uid uid
     * @param request 请求
     * @return 返回 1 表示删除成功
     */
    @RequestMapping("delart")
    public int delArticle(Integer aid, Integer uid, HttpServletRequest request) {
        int data = 0;
        // 得到当前登录用户
        UserInfo userInfo = (UserInfo) request.getSession(false).getAttribute(ConstVariable.USER_SESSION_KEY);
        // 判断当前登录用户，并且判断只能删除自己的文章
        if (userInfo != null && userInfo.getUid() == uid) {
            // 判断aid合法性并删除该文章
            if (aid > 0) {
                data = articleService.delArticle(aid);
            }
        }
        return data;
    }

    /**
     * 更新文章
     * @param title 标题
     * @param context 正文
     * @param aid aid
     * @param request 请求
     * @return 更新成功返回 1
     */
    @RequestMapping("/upart")
    public int updateArticle(String title, String context, Integer aid, HttpServletRequest request) {
        int data = 0;
        // 得到当前登录用户
        UserInfo userInfo = (UserInfo) request.getSession(false).getAttribute(ConstVariable.USER_SESSION_KEY);
        if (userInfo != null) {
            // 效验前端参数
            if (StringUtils.hasLength(title) && StringUtils.hasLength(context) && aid > 0) {
                data = articleService.updateArticle(title, context, aid, userInfo.getUid());
            }
        }
        return data;
    }

    /**
     * 根据 aid 查询用户信息
     * @param aid aid
     * @return 用户信息
     */
    @RequestMapping("/getuserbyaid")
    public UserInfo getUserByAid(Integer aid) {
        UserInfo userInfo = new UserInfo();
        if (aid > 0) {
            userInfo = articleService.getUserByAid(aid);
        }
        return userInfo;
    }

    /**
     * 根据 aid 查询用户文章数量
     * @param aid aid
     * @return 用户文章数量
     */
    @RequestMapping("/getarticlecount")
    public int getArticleCount(Integer aid) {
        int data = -1;
        if (aid > 0) {
            data = articleService.getArticleCount(aid);
        }
        return data;
    }
}
